import { app, BrowserWindow, Menu, nativeImage, Tray } from 'electron';
import path from 'path';

import { enable, initialize } from '@electron/remote/main';
import { gen } from 'src/api/ConfigApi';

initialize();
let tray;
let mainWindow: BrowserWindow | undefined;

function createWindow() {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    width: 1200,
    height: 800,
    minWidth: 1000,
    minHeight: 600,
    useContentSize: true,
    fullscreen: false,
    frame: false,
    transparent: false,
    show: false,
    webPreferences: {
      contextIsolation: true,
      nodeIntegration: true,
      webSecurity: false, // More info: https://v2.quasar.dev/quasar-cli-vite/developing-electron-apps/electron-preload-script
      preload: path.resolve(__dirname, process.env.QUASAR_ELECTRON_PRELOAD),
    },
  });
  enable(mainWindow.webContents);
  mainWindow.loadURL(process.env.APP_URL);
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  if (process.env.DEBUGGING) {
    mainWindow.webContents.openDevTools();
    // if on DEV or Production with debug enabled
  } else {
    // we're on production; no access to devtools pls
    mainWindow.webContents.on('devtools-opened', () => {
      mainWindow?.webContents.closeDevTools();
    });
  }
  mainWindow.setThumbarButtons([
    {
      tooltip: 'button1',
      icon: nativeImage.createFromPath(
        path.join(__dirname, './icons/icon.png')
      ),
      click() {
        console.log('button1 clicked');
      },
    },
    {
      tooltip: 'button2',
      icon: nativeImage.createFromPath(
        path.join(__dirname, './icons/icon.png')
      ),
      flags: ['enabled', 'dismissonclick'],
      click() {
        console.log('button2 clicked.');
      },
    },
  ]);
  mainWindow.on('closed', () => {
    mainWindow = undefined;
  });
  mainWindow.setMenu(null);

  mainWindow.setThumbarButtons([
    {
      tooltip: 'button1',
      icon: nativeImage.createFromPath(
        path.join(__dirname, './icons/icon.png')
      ),
      click() {
        console.log('button1 clicked');
      },
    },
    {
      tooltip: 'button2',
      icon: nativeImage.createFromPath(
        path.join(__dirname, './icons/icon.png')
      ),
      flags: ['enabled', 'dismissonclick'],
      click() {
        console.log('button2 clicked.');
      },
    },
  ]);
  // mainWindow.setOverlayIcon(nativeImage.createFromPath('public/icon.ico'), '喵喵喵')

  mainWindow.once('ready-to-show', () => {
    mainWindow?.show();
  });

  app.setAppUserModelId('浅蓝喵喵喵');
  app.setJumpList([
    {
      type: 'custom',
      name: 'Recent Projects',
      items: [
        { type: 'file', path: 'C:\\Projects\\project1.proj' },
        { type: 'file', path: 'C:\\Projects\\project2.proj' },
      ],
    },
    {
      // has a name so `type` is assumed to be "custom"
      name: 'Tools',
      items: [
        {
          type: 'task',
          title: 'Tool B',
          program: process.execPath,
          args: '--run-tool-b',
          iconPath: process.execPath,
          iconIndex: 0,
          description: 'Runs Tool B',
        },
      ],
    },
    { type: 'frequent' },
    {
      // has no name and no type so `type` is assumed to be "tasks"
      items: [
        {
          type: 'task',
          title: 'New Project',
          program: process.execPath,
          args: '--new-project',
          description: 'Create a new project.',
        },
        { type: 'separator' },
        {
          type: 'task',
          title: 'Recover Project',
          program: process.execPath,
          args: '--recover-project',
          description: 'Recover Project',
        },
      ],
    },
  ]);
}

app.commandLine.appendSwitch('ignore-certificate-errors');
app.whenReady().then(() => {
  createWindow();
  const icon = nativeImage.createFromPath(
    path.join(__dirname, './icons/icon.png')
  );
  tray = new Tray(icon);
  const contextMenu = Menu.buildFromTemplate([
    // { role: 'appMenu' }

    {
      label: app.name,
      submenu: [
        { role: 'about' },
        { type: 'separator' },
        { role: 'services' },
        { type: 'separator' },
        { role: 'hide' },
        { role: 'hideOthers' },
        { role: 'unhide' },
        { type: 'separator' },
        { role: 'quit' },
        {
          label: 'asdas',
          click: async () => {
            await window.ipcCom.test();
          },
        },
      ],
    },
  ]);

  tray.setContextMenu(contextMenu);
  tray.setToolTip('喵喵喵');
  tray.setTitle('呱呱呱');
});

app.on('activate', () => {
  if (mainWindow === undefined) {
    createWindow();
  }
});
